/**
 * 
 */
package us.gaaoc.framework.util.validators;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

import us.gaaoc.framework.webapp.util.Messages;

/**
 * @author mcelratr
 *
 */
public class DateValidator {
	


	/**
	 * Method used to validate a string that represents a date in MM/dd/YY 
	 * format.
	 * 
	 * @param date
	 * @return
	 */
	public boolean validateDate(String date) {
		//String dateFormat = "yyyy-MM-dd";
		String dateFormat = "MM/dd/yy";
		Boolean valid = false;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
			sdf.setLenient(false);
			ParsePosition pos = new ParsePosition(0);
			Date dt2 = sdf.parse(date, pos);
			// Check possible things that signal a parsing error
			if ((date == null) || (pos.getErrorIndex() != -1)
					|| date.length() != 8) {
				valid = false;
				System.out.println("Error: " + pos.getIndex());
				if (date == null) {
					valid = false;
					System.out.println("Date is null");
				}
				if (pos.getErrorIndex() != -1) {
					valid = false;
					System.out.println("Error index: " + pos.getErrorIndex());
				}
				// YYYY-MM-DD = 10 characters
				// MM/dd/yy = 8 characters
				if (date.length() != 8) {
					valid = false;
					System.out.println("Error: Date has too few or too many "
							+ "characters");
				}
			} else {
				valid = true;
				System.out.println("Date appears to be valid.");
				System.out.println("Date is ok = " + dt2 + "(" + date + ")");
			}
		//} catch (ParseException e) {
			//System.out.println(e.getMessage());
		} catch (IllegalArgumentException e) {
			System.out.println("Invalid date");
		}
		return valid;
	} // End of validateDateMethod
	
	/**
	 * Method used to validate a string that represents a date in MM/dd/YY 
	 * format.
	 * 
	 * @param date
	 * @param dateFormatPassedIn The date format to be validated.  For example, 
	 * MM/dd/YY is a representation of a date such as 12/25/07.  yyyy-MM-dd is 
	 * an example of a date that looks like 2007-12-25. Reference is the Simple
	 * Date Format API page at 
	 * <a href=http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html>
	 * http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html
	 * </a>
	 * @return
	 */
	public boolean validateDate(String date, String dateFormatPassedIn) {
	
		String dateFormat = dateFormatPassedIn;
		Boolean valid = false;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
			sdf.setLenient(false);
			ParsePosition pos = new ParsePosition(0);
			Date dt2 = sdf.parse(date, pos);
			// Check possible things that signal a parsing error
			if ((date == null) || (pos.getErrorIndex() != -1)
					|| date.length() != 8) {
				valid = false;
				System.out.println("Error: " + pos.getIndex());
				if (date == null) {
					valid = false;
					System.out.println("Date is null");
				}
				if (pos.getErrorIndex() != -1) {
					valid = false;
					System.out.println("Error index: " + pos.getErrorIndex());
				}
				// YYYY-MM-DD = 10 characters
				// MM/dd/yy = 8 characters
				if (date.length() != 8) {
					valid = false;
					System.out.println("Error: Date has too few or too many "
							+ "characters");
				}
			} else {
				valid = true;
				System.out.println("Date appears to be valid.");
				System.out.println("Date is ok = " + dt2 + "(" + date + ")");
			}
		//} catch (ParseException e) {
			//System.out.println(e.getMessage());
		} catch (IllegalArgumentException e) {
			System.out.println("Invalid date");
		}
		return valid;
	} // End of validateDateMethod
	
	

}
